//
//  CZWelcomeViewController.swift
//  微博
//
//  Created by 范承志 on 16/6/11.
//  Copyright © 2016年 fanchengzhi. All rights reserved.
//

import UIKit
import SDWebImage

class CZWelcomeViewController: UIViewController {

    override func loadView() {
        
        view = bgImageView
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        //设置headImageView动画
        //修改top约束
        self.headImageView.snp_updateConstraints { (make) in
            
            make.top.equalTo(self.view).offset(100)
        }
        //设置动画
        UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
            
            //重新更新约束
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
                //显示欢迎回来Label
                UIView.animate(withDuration: 0.25, animations: { 
                    
                    self.messageLabel.alpha = 1
                    },completion:{(_) in
                        
                        //发通知切换根控制器
                        NotificationCenter.default.post(name: NSNotification.Name(rawValue:CZSWITCHROOTVCNOTI), object: "welcome")
                        
                })
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    //设置视图
    private func setupUI(){
        
        //添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        //添加约束
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
    }
    
    //懒加载控件
    //背景图片
    private lazy var bgImageView: UIImageView = UIImageView(imageName: "ad_background")
    
    //用户头像
    private lazy var headImageView: UIImageView = {
        
        let image = UIImageView()
        image.layer.borderColor = CZTHEMECOLOR.cgColor
        image.layer.borderWidth = 1
        //切圆角
        image.layer.cornerRadius = 45
        image.layer.masksToBounds = true
        image.CZ_setImage(withUrlString: CZUserAccountViewModel.sharedTools.userAccountModel?.avatar_large, placeholderImageName: "avatar_default_big")
        return image
    }()
    
    //欢迎回来
    private lazy var messageLabel: UILabel = {
        
        let lab = UILabel(textColor: CZTHEMECOLOR, fontSize: 18, text: "欢迎回来")
        //对齐方式
        lab.textAlignment = .center
        lab.alpha = 0
        return lab
    }()
}
